package com.ins1st.plus.service.system.role.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ins1st.plus.api.system.menu.entity.Ztree;
import com.ins1st.plus.api.system.role.entity.SysRole;
import com.ins1st.plus.api.system.role.entity.SysRoleMenu;
import com.ins1st.plus.api.system.role.entity.SysUserRole;
import com.ins1st.plus.api.system.role.service.SysRoleApi;
import com.ins1st.plus.api.system.role.service.SysRoleMenuApi;
import com.ins1st.plus.api.system.role.service.SysUserRoleApi;
import com.ins1st.plus.base.ServiceResult;
import com.ins1st.plus.service.role.mapper.SysRoleMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author sun
 * @since 2019-09-07
 */
@Service(interfaceClass = SysRoleApi.class, timeout = 3000)
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleApi {

    @Autowired
    SysUserRoleApi sysUserRoleApi;
    @Autowired
    SysRoleMenuApi sysRoleMenuApi;

    @Override
    public ServiceResult queryRolesByUserId(Integer userId) {
        List<SysUserRole> sysUserRoleList = sysUserRoleApi.list(new QueryWrapper<SysUserRole>().eq("user_id", userId));
        List<SysRole> sysRoleList = this.baseMapper.selectList(null);
        List<Ztree> ztrees = new ArrayList<>();
        for (SysRole sysRole : sysRoleList) {
            Ztree ztree = new Ztree();
            ztree.setId(sysRole.getId());
            ztree.setpId(0);
            ztree.setName(sysRole.getRoleName());
            for (SysUserRole sysUserRole : sysUserRoleList) {
                if (sysUserRole.getRoleId().equals(sysRole.getId())) {
                    ztree.setChecked(true);
                }
            }
            ztrees.add(ztree);

        }
        return ServiceResult.success(ztrees);
    }

    @Override
    public ServiceResult addUserRole(Integer userId, String roleIds) {
        sysUserRoleApi.remove(new QueryWrapper<SysUserRole>().eq("user_id", userId));
        String[] ids = roleIds.split(",");
        for (String roleId : ids) {
            if (StringUtils.isNotBlank(roleId)) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setRoleId(Integer.valueOf(roleId));
                sysUserRole.setUserId(userId);
                sysUserRoleApi.save(sysUserRole);
            }
        }
        return ServiceResult.success();
    }

    @Override
    public IPage<SysRole> page(SysRole sysRole, Page page) {
        QueryWrapper qw = new QueryWrapper();
        qw.setEntity(sysRole);
        return this.baseMapper.selectPage(page, qw);
    }

    @Override
    public ServiceResult addRoleMenus(Integer roleId, String menuIds) {
        sysRoleMenuApi.remove(new QueryWrapper<SysRoleMenu>().eq("role_id", roleId));
        String[] ids = menuIds.split(",");
        for (String menuId : ids) {
            if (StringUtils.isNotBlank(menuId)) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(Integer.valueOf(menuId));
                sysRoleMenu.setRoleId(roleId);
                this.sysRoleMenuApi.save(sysRoleMenu);
            }
        }
        return ServiceResult.success();
    }
}
